package com.example.erat.controller;

import com.example.erat.common.Result;
import com.example.erat.dto.SubmissionQueryDTO;
import com.example.erat.entity.Submission;
import com.example.erat.service.SubmissionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Api(tags = "实验提交管理接口")
@RestController
@RequestMapping("/submission")
public class SubmissionController {

    @Autowired
    private SubmissionService submissionService;

    @ApiOperation("学生提交实验报告")
    @PostMapping("/submit")
    public Result<String> submitExperiment(
            @RequestParam("studentId") int studentId,
            @RequestParam("experimentId") int experimentId,
            @RequestParam("file") MultipartFile file) {
        submissionService.submitExperiment(studentId, experimentId, file);
        return Result.success("提交成功");
    }

    @ApiOperation("教师获取提交列表")
    @PostMapping("/list")
    public Result<List<Submission>> getSubmissionList(@RequestBody SubmissionQueryDTO queryDTO) {
        List<Submission> submissions = submissionService.getSubmissionList(queryDTO);
        return Result.success(submissions);
    }

    @ApiOperation("教师评分")
    @PostMapping("/grade")
    public Result<String> gradeSubmission(
            @RequestParam("submissionId") String submissionId,
            @RequestParam("score") Integer score,
            @RequestParam("feedback") String feedback) {
        submissionService.gradeSubmission(submissionId, score, feedback);
        return Result.success("评分成功");
    }

    @ApiOperation("下载实验报告")
    @GetMapping("/download/{submissionId}")
    public void downloadFile(@PathVariable String submissionId, HttpServletResponse response) {
        submissionService.downloadFile(submissionId, response);
    }
}    